package com.longyiqun.cm.controller.download;

import com.alibaba.fastjson.JSON;
import com.longyiqun.cm.PageUtil;
import com.longyiqun.cm.TablePage;
import com.longyiqun.cm.controller.cases.CasesRestControllerImpl;
import com.longyiqun.cm.service.FileService;

import org.apache.catalina.User;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/download")
public class FileRestcontrollerImpl {

    @Autowired
    PageUtil pageUtil;

    @Autowired
    FileService fileService ;

    @RequestMapping("/getFileForPaging")
    public String getfileForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize)
    {
        System.out.println("fileService::::"+fileService.getFileForPaging(pageNumber, pageSize));
            TablePage tp = pageUtil.getDataForPaging(fileService.getFileForPaging(pageNumber, pageSize));
            return JSON.toJSONString(tp);
    }

    @RequestMapping(value="/uploadFile")
    public String readExcel(@RequestParam(value="file") MultipartFile file, HttpServletRequest request, HttpSession session) throws IOException {
        ModelAndView mv = new ModelAndView();
        String path=request.getServletContext().getContextPath();
        System.out.println(path);
        //判断文件是否为空
        if (file == null) {
            mv.addObject("msg", "failed");
            mv.setViewName("excel_result");
        }
        String resource = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        resource = StringUtils.substringBefore(resource,"target") +"src/main/resources/static/file/";
        //获取输出流
        try {

            OutputStream os = new FileOutputStream(resource + new Date().getTime() + file.getOriginalFilename());
            //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
            InputStream is = file.getInputStream();
            int temp;
            //一个一个字节的读取并写入
            while ((temp = is.read()) != (-1)) {
                os.write(temp);
            }
            os.flush();
            os.close();
            is.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "/success";
    }

    @RequestMapping(value="/upload")
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("===============");
        //如果文件不为空，写入上传路径
        if(!file.isEmpty()) {
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            path = StringUtils.substringBefore(path,"target") +"src/main/resources/static/file/";
            //上传文件名
            String filename = file.getOriginalFilename();
            File NewFile = new File(path + File.separator + filename);
            //将上传文件保存到一个目标文件当中
            file.transferTo(NewFile);
            InputStream is = new FileInputStream(NewFile);

                    }



    return "";
    }




}

